<?php

namespace app\admin\model;

use think\Model;

use think\Db;
use think\Request;

class Banks extends Model
{
    public function getbanks($res)
    {
        $page = $res['page'];
        $limit = $res['pageSize'];
        $data = $this->page($page, $limit)->select();
        foreach ($data as $datakey => $dataval) {
            $data[$datakey]['countryname'] = getinfo($dataval['country_id'], 'country')['name'];
        }
        $count = $this->count();
        return array('count' => $count, 'data' => $data);
    }

    public function addeditbanks($data)
    {
        $ckdata = notEmpty(['country_id' => lang("country"), 'name' => lang("bank_name"), 'shortname' => lang("bank_abbreviation"), 'code' => lang("bank_code")]);
        if ($ckdata['status']==2) {
            return $ckdata;
        }
        $act = $data['act'];
        unset($data['act']);
        if ($act == 'banksadd') {
            unset($data['id']);
            $data['create_id'] = $data['update_id'] = session('userid');
            $data['create_at'] = $data['update_at'] = time();
            $banksid = $this->insert($data);
            if ($banksid) {
                return array('msg' => $data['name'] . lang("added_successfully"), 'status' => 1);
            } else {
                return array('msg' => $data['name'] . lang("add_failed"), 'status' => 0);
            }
        } elseif ($act == 'banksedit') {
            $data['update_id'] = session('userid');
            $data['update_at'] = time();
            $editban = $this->where('id', $data['id'])->update($data);
            if ($editban !== false) {
                return array('msg' => $data['name'] . lang("edited_successfully"), 'status' => 1);
            } else {
                return array('msg' => $data['name'] . lang("edit_failed"), 'status' => 0);
            }
        }

    }

    //银行的删除
    public function delbanks($data)
    {
        $name = $data['name'];
        $accountid=Db::name('accounts')->where('banks_id',$data['id'])->find();
        if ($accountid){
            return array('msg' => $name . lang("account_containing_the_payment_can't_delete"), 'status' => 0);
        }
        $cobanksid=Db::name('co_bank')->where('banks_id',$data['id'])->find();
        if ($cobanksid){
            return array('msg' => $name . lang("it_contains_company_bank_account_can't_be_deleted"), 'status' => 0);
        }
        $delid = $this->where('id', $data['id'])->delete();
        if ($delid) {
            return array('msg' => $name . lang("deleted_successfully"), 'status' => 1);
        } else {
            return array('msg' => $name . lang("failed_to_delete"), 'status' => 0);
        }
    }
}